<?php

namespace app\admin\controller\group;


use app\common\controller\AdminController;
use app\common\services\annotation\ControllerAnnotation;
use app\common\services\annotation\NodeAnnotation;

//
use support\Request; //请求类
use support\Response; //请求类
use think\facade\Db; //数据库类
use think\App; //初始化
use think\Exception; //异常
 
/**
 * @ControllerAnnotation(title="group_chat")
 */
class ChatController extends AdminController
{

    public function initialize()
    {
         parent::initialize();
         $this->model = new \app\admin\model\GroupChat();
    }
    
    public function index(Request $request): Response{
        $gfid = $request->get('id');
        if(!$gfid){
           $gfid = "-1" ;
        }
        if (!$request->isAjax()) return view('admin/group/chat/index',['gfid'=>$gfid]);
        if ($request->input('selectFields')) {
            return $this->selectList();
        }
        
        list($page, $limit, $where) = $this->buildTableParams();


        $count = $this->model->where($where)->count();
        $list  = $this->model->where($where)->order($this->order)->page($page, $limit)->select()->toArray();
        foreach($list as &$ls){
            $ls['fname'] = DB::name('group_list')->where('id',$ls['fid'])->value('title');
            if(!$ls['fname'] or empty($ls['fname'])){
                $ls['fname'] = "群聊不存在";
            }
            $ls['user'] = DB::name('user')->where('id',$ls['userid'])->value('nickname');
            if(!$ls['user'] or empty($ls['user'])){
                $ls['user'] = "用户不存在";
            }
        }
        $data  = [
            'code'  => 0,
            'msg'   => '',
            'count' => $count,
            'data'  => $list,
        ];
        return json($data);
    }
}
